package net.oschina.dajiangnan.tree;


/**
 * 根节点增加traceInfo和异常处理逻辑的抽象节点
 */
public abstract class AbstractNode<T extends AbstractContext> implements Node<T> {

    protected abstract void processBusiness(T t) throws Exception;

    protected abstract Node selectNode(T t);

    @Override
    public Node process(T t) {
        long start = System.currentTimeMillis();
        try {
            processBusiness(t);
            return selectNode(t);
        } catch (Exception e) {
            return new ExceptionNode(e);
        } finally {
            long last = System.currentTimeMillis() - start;
            t.getNodeTrace().add(new AbstractContext.TraceInfo(this.getClass(), last));
        }
    }
}
